<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>评论回车发布</title>
  <style>
    .wrapper {
      min-width: 400px;
      max-width: 800px;
      display: flex;
      justify-content: flex-end;
    }

    .avatar {
      width: 48px;
      height: 48px;
      border-radius: 50%;
      overflow: hidden;
      /* background: url(./images/avatar.jpg) no-repeat center / cover; */
      margin-right: 20px;
    }

    .wrapper textarea {
      outline: none;
      border-color: transparent;
      resize: none;
      background: #f5f5f5;
      border-radius: 4px;
      flex: 1;
      padding: 10px;
      transition: all 0.5s;
      height: 30px;
    }

    .wrapper textarea:focus {
      border-color: #e4e4e4;
      background: #fff;
      height: 50px;
    }

    .wrapper button {
      background: #00aeec;
      color: #fff;
      border: none;
      border-radius: 4px;
      margin-left: 10px;
      width: 70px;
      cursor: pointer;
    }

    .wrapper .total {
      margin-right: 80px;
      color: #999;
      margin-top: 5px;
      opacity: 0;
      transition: all 0.5s;
    }

    .list {
      min-width: 400px;
      max-width: 800px;
      /* display: flex; */
    }

    .list .item {
      width: 100%;
      display: flex;
    }

    .list .item .info {
      flex: 1;
      border-bottom: 1px dashed #e4e4e4;
      padding-bottom: 10px;
    }

    .list .item p {
      margin: 0;
    }

    .list .item .name {
      color: #FB7299;
      font-size: 14px;
      font-weight: bold;
    }

    .list .item .text {
      color: #333;
      padding: 10px 0;
    }

    .list .item .time {
      color: #999;
      font-size: 12px;
    }
  </style>
  <script src="../js/jquery.min.js"></script>

</head>

<body>
  <form class="wrapper">
    <i class="avatar"></i>
    评论人: <input type="text" name="username">
    <textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200" name="content"></textarea>
    <button type="submit">发布</button>
  </form>
  <div class="wrapper">
    <span class="total">0/200字</span>
  </div>
  <div class="list">

  </div>
  <script>

    const tx = document.querySelector('#tx')
    const total = document.querySelector('.total')
    // 1. 当我们文本域获得了焦点，就让 total 显示出来
    tx.addEventListener('focus', function () {
      total.style.opacity = 1
    })
    // 2. 当我们文本域失去了焦点，就让 total 隐藏出来
    tx.addEventListener('blur', function () {
      total.style.opacity = 0
    })
    // 3. 检测用户输入
    tx.addEventListener('input', function () {
      // console.log(tx.value.length)  得到输入的长度
      total.innerHTML = `${tx.value.trim().length}/200字`
    })

    $(() => {
      const renderer = () => {
        $.ajax({
          type: 'GET',
          url: 'http://www.liulongbin.top:3006/api/cmtlist',
          success: res => {
            if (res.status !== 200) return alert('获取失败')

            const data = res.data
            $('.list').html(
              data.map(item => {
                const { username, content, time } = item
                return `
              <div class="item">
                <i class="avatar"></i>
                <div class="info">
                  <p class="name">${username}</p>
                  <p class="text">${content}</p>
                  <p class="time">${time}</p>
                </div>
              </div>
              `
              }).join(''))
          }
        })
      }
      renderer()

      $('form').on('submit', function (e) {
        e.preventDefault()
        $.ajax({
          type: 'POST',
          url: 'http://www.liulongbin.top:3006/api/addcmt',
          data: $(this).serialize(),
          success: res => {
            if (res.status !== 201) return alert('发布失败')
            this.reset()
            renderer()
          }
        })
      })
    })
    // setInterval(() => {
    //   $.ajax({
    //     type: 'POST',
    //     url: 'http://www.liulongbin.top:3006/api/addcmt',
    //     data: {
    //       // username: '长沙黑马55期lwj',
    //       // content: '在线征婚'
    //       username: '长沙黑马',
    //       content: '111'
    //     },
    //     success: res => {
    //       console.log(res)
    //     }
    //   })
    // }, 200)

  </script>
</body>

</html>